﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.SystemConfig.ArchiveCode
{
    /// <summary>
    /// 备注：档号生成帮助类
    /// 时间：2012年12月3日 22:37:12
    /// </summary>
    public class ArchiveCodeGenerateHelper
    {
        public static int GetCurrArchiveCodeNum(WorkFlow.ArchiveTableEnum archiveType,string quanzonghao, string wellcode = null)
        {
            ArchiveCodeNumberDAL _archiveCodeNumberDal = new ArchiveCodeNumberDAL();
            return _archiveCodeNumberDal.GetNextNumber(archiveType, quanzonghao, wellcode);
        }

        /// <summary>
        /// 生成档号
        /// </summary>
        /// <param name="archiveType"></param>
        /// <param name="quanzonghao"></param>
        /// <param name="jh">井号-勘探开发单井类档号生成时传入</param>
        /// <returns></returns>
        public static string Generate(WorkFlow.ArchiveTableEnum archiveType,string quanzonghao, string jh, ref int archiveCodeNumber )
        {
            ArchiveCodeNumberDAL _archiveCodeNumberDal = new ArchiveCodeNumberDAL();
            string archiveCodeFmt = string.Empty;

            if (archiveCodeNumber <= 0)
            {
                archiveCodeNumber = _archiveCodeNumberDal.GetNextNumber(archiveType, quanzonghao, jh);
            }
            
            //2013-04-30 添加 
            //档号生成新规则
            string seriesNumber = archiveCodeNumber.ToString();
            if (seriesNumber.Length < 4)
                seriesNumber = seriesNumber.PadLeft(4, '0');

            switch(archiveType)
            {
                //************************ W-0000001
                case WorkFlow.ArchiveTableEnum.Document:
                    archiveCodeFmt = "{0}-W-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.CDKind:
                    archiveCodeFmt = "{0}-E-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.Technology:
                    archiveCodeFmt = "{0}-F-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.BulidProject:
                    archiveCodeFmt = "{0}-G-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.Equipment:
                    archiveCodeFmt = "{0}-H-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.OliSearchK01:
                    archiveCodeFmt = "{0}-K01-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.RealKindR01:
                    archiveCodeFmt = "{0}-R01-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;

                //*****************会计类******************* I01-2012-0001      I02-0000001
                case WorkFlow.ArchiveTableEnum.AccountI01:
                    archiveCodeFmt = "{0}-I01-{1}-{2}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, DateTime.Now.Year, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.AccountI02:
                    archiveCodeFmt = "{0}-I02-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.AccountI03:
                    archiveCodeFmt = "{0}-I03-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.AccountI04:
                    archiveCodeFmt = "{0}-I04-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;

                //*****************勘探开发单井类************* K-井号-001
                case WorkFlow.ArchiveTableEnum.OliSearchK02:
                    archiveCodeFmt = "{0}-K02-{1}-{2}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, jh, seriesNumber);
                    break;

                //*****************声像类******************** S03-0000001
                case WorkFlow.ArchiveTableEnum.AudioVedioS01:
                    archiveCodeFmt = "{0}-S01-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.AudioVedioS02:
                    archiveCodeFmt = "{0}-S02-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
                case WorkFlow.ArchiveTableEnum.AudioVedioS03:
                    archiveCodeFmt = "{0}-S03-{1}";
                    archiveCodeFmt = string.Format(archiveCodeFmt, quanzonghao, seriesNumber);
                    break;
            }
            return archiveCodeFmt;  
        }

        /// <summary>
        /// 更新指定档案类别的流水号
        /// </summary>
        /// <param name="archiveType"></param>
        /// <param name="currentNumber"></param>
        public static void UpdateArchiveCodeNumber(WorkFlow.ArchiveTableEnum archiveType,string qzh, int currentNumber, string wellcode = null)
        {
            ArchiveCodeNumberDAL _archiveCodeNumberDal = new ArchiveCodeNumberDAL();
            _archiveCodeNumberDal.UpdateCurrentNumber(archiveType,qzh, ++currentNumber, wellcode);
        }
    }
}
